package com.yang.library.client.menu;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.yang.library.bookenum.State;
import com.yang.library.bookenum.TCPEnum;
import com.yang.library.entry.User;
import com.yang.library.util.AppContext;
import com.yang.library.util.Constant;
import com.yang.library.util.PrintUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class UserManagementMenu implements Menu {
    private final ObjectMapper mapper = AppContext.getInstance().getMapper();
    private final Socket socket;

    public UserManagementMenu(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void start() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
            while (true) {
                System.out.println("=========欢迎来到用户管理界面==========");
                System.out.println("1. 查看用户");
                System.out.println("2. 修改密码");
                System.out.println("3. 退出用户管理界面");
                int type = PrintUtils.getIntInput(AppContext.getInstance().getScanner(), "请输入您的选择:");
                switch (type) {
                    case 1:
                        getUserByName(reader, writer);
                        break;
                    case 2:
                        changePassword(reader, writer);
                        break;
                    case 3:
                        return;
                    default:
                        System.out.println("输入错误，请重新输入！");
                }
            }
        } catch (Exception e) {
            System.out.println("用户管理界面异常！");
            throw new RuntimeException(e);
        }
    }

    private void changePassword(BufferedReader reader, BufferedWriter writer) throws Exception {
        String oldPassword = PrintUtils.getInput("请输入旧密码:");
        String newPassword = PrintUtils.getInput("请输入新密码:");
        writer.write(mapper.writeValueAsString(TCPEnum.USER_CHANGE_PASSWORD) + Constant.NEW_LINE);
        writer.write(mapper.writeValueAsString(oldPassword) + Constant.NEW_LINE);
        writer.write(mapper.writeValueAsString(newPassword) + Constant.NEW_LINE);
        writer.write(mapper.writeValueAsString(AppContext.getInstance().getUserName()) + Constant.NEW_LINE);
        writer.flush();

        // 等待服务器响应
        String readMsg = reader.readLine();
        if (readMsg == null) return;
        State state = mapper.readValue(readMsg, State.class);
        switch (state) {
            case SUCCESS:
                System.out.println("密码修改成功！");
                break;
            case FAILURE:
                System.out.println("密码修改失败！");
                break;
        }
    }

    private void getUserByName(BufferedReader reader, BufferedWriter writer) throws Exception {
        writer.write(mapper.writeValueAsString(TCPEnum.USER_GET_USER_BY_NAME) + Constant.NEW_LINE);
        writer.write(mapper.writeValueAsString(AppContext.getInstance().getUserName()) + Constant.NEW_LINE);
        writer.flush();

        // 等待服务器响应
        String readMsg = reader.readLine();
        if (readMsg == null) return;
        User user = mapper.readValue(readMsg, User.class);
        if (user == null) {
            System.out.println("用户不存在！");
            return;
        }
        PrintUtils.printUsers(List.of(user));
    }
}
